# --- 构建阶段 ---
FROM nvcr.io/nvidia/l4t-jetpack:r35.4.1 AS builder 

# 设置代理（如果需要编译阶段使用）
RUN git config --global http.proxy socks5://127.0.0.1:7897 && \
    git config --global https.proxy socks5://127.0.0.1:7897

# 更新源并安装编译工具和库的构建依赖
RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \
    sed -i 's|http://ports.ubuntu.com/ubuntu-ports/|https://repo.huaweicloud.com/ubuntu-ports/|g' /etc/apt/sources.list && \
    sed -i 's|http://archive.ubuntu.com/ubuntu|https://repo.huaweicloud.com/ubuntu-ports|g' /etc/apt/sources.list && \
    sed -i 's|http://security.ubuntu.com/ubuntu/|https://repo.huaweicloud.com/ubuntu-ports/|g' /etc/apt/sources.list && \
    apt update -y && \
    apt install -y libeigen3-dev libopenni2-dev lsb-release curl git python3-pip cmake build-essential libglew-dev libboost-dev libboost-thread-dev libboost-filesystem-dev mlocate
# ROS 安装（这里可以在构建阶段完成，只复制最终的 ROS 环境文件到运行时阶段）
RUN sh -c '. /etc/lsb-release && echo "deb http://mirrors.sjtug.sjtu.edu.cn/ros/ubuntu/ `lsb_release -cs` main" > /etc/apt/sources.list.d/ros-latest.list' && \
    curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - && \
    apt update -y && \
    apt install ros-noetic-desktop-full -y && \
    apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool -y



WORKDIR /home

# 安装 Pangolin
RUN git clone https://gitee.com/boboji11/pangolin-v0.5-modified.git && \
    cd pangolin-v0.5-modified && \
    chmod +x build.sh && \
    ./build.sh

# 安装 OpenCV 3.4.5
RUN git clone https://gitee.com/boboji11/opencv3.4.5-modified.git && \
    cd opencv3.4.5-modified && \
    chmod +x build.sh && \
    ./build.sh

# 编译 ORBSLAM2_with_pointcloud_map 和其依赖
RUN git clone https://gitee.com/boboji11/orb-slam2-pointcloud_map-modified.git && \
    cd orb-slam2-pointcloud_map-modified && \
    cd g2o_with_orbslam2 && chmod +x build.sh && ./build.sh && \
    cd ../ORB_SLAM2_modified && chmod +x build_all.sh && ./build_all.sh

# --- 运行时阶段 ---
FROM nvcr.io/nvidia/l4t-jetpack:r35.4.1 

# 复制 ROS 环境（如果 ROS 安装在标准路径，可能不需要明确复制，而是设置环境变量）
# 这里只是一个示例，具体复制哪些文件取决于 ROS 的安装方式
# COPY --from=builder /opt/ros/noetic /opt/ros/noetic
# RUN echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc

# 复制 Pangolin 的安装结果
COPY --from=builder /usr/local/include/pangolin /usr/local/include/pangolin
COPY --from=builder /usr/local/lib/libpangolin* /usr/local/lib/

# 复制 OpenCV 的安装结果
COPY --from=builder /usr/local/opencv3.4.5 /usr/local/opencv3.4.5
RUN echo "/usr/local/opencv3.4.5/lib" >> /etc/ld.so.conf.d/opencv.conf && \
    ldconfig && \
    echo "export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:/usr/local/opencv3.4.5/lib/pkgconfig" >> ~/.bashrc && \
    echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/opencv3.4.5/lib" >> ~/.bashrc

# 复制 ORBSLAM2_with_pointcloud_map 的编译结果
COPY --from=builder /home/orb-slam2-pointcloud_map-modified /home/orb-slam2-pointcloud_map-modified

# 安装运行时必要的依赖（如果某些依赖在构建阶段安装了，但运行时也需要，则需要再次安装）
RUN apt update -y && apt install -y libeigen3-dev libopenni2-dev lsb-release curl libglew-dev libboost-thread-dev libboost-filesystem-dev

# 设置 ROS 环境变量（如果 ROS 不在 /opt/ros/noetic 中，需要调整路径）
ENV ROS_PACKAGE_PATH="/home/orb-slam2-pointcloud_map-modified/ORB_SLAM2_modified/Examples/ROS:${ROS_PACKAGE_PATH}"
RUN echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc

# 清理 APT 缓存
RUN apt clean && rm -rf /var/lib/apt/lists/*

WORKDIR /home/orb-slam2-pointcloud_map-modified/ORB_SLAM2_modified

# 最后一个 RUN 命令可以用来进行运行时测试或者设置入口点
# 例如，您可以将数据集下载放在这里，或者通过 ENTRYPOINT/CMD 在运行时下载
# RUN curl -x socks5://127.0.0.1:7897 https://cvg.cit.tum.de/rgbd/dataset/freiburg1/rgbd_dataset_freiburg1_desk.tgz -o rgbd_dataset_freiburg1_desk.tgz && \
#    tar -xvf rgbd_dataset_freiburg1_desk.tgz -C dataset